package com.zxmr.hospital.info.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.ConsultWay;
import com.zxmr.domain.service.ConsultService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;

public class ConsultWayAction extends Executor{

	enum method{
		add,
		delete,
		queryAll,
		update
	}
	@Override
	public void execute(Request request, Response response) {
		// TODO Auto-generated method stub
		method m = Enum.valueOf(method.class, this.getElement().getMethod());
		try
		{
			switch(m)
			{
			case add:
				add(request,response);
				break;
			case queryAll:
				queryAll(request,response);
				break;
			case update:
				update(request,response);
				break;
			case delete:
				delete(request,response);
				break;
			}
		}catch(Exception e)
		{
			if(e instanceof ExecuteException)
			{
				ResponsePayload payload = new ResponsePayload(e.getMessage());
				response.setPayload(payload);
				response.setResult(ResultCode.COMPONENT_ERROR);
			}else
			{
				e.printStackTrace();
				this.internalErrorResponse(response);
			}
		}
	}

	public void delete(Request request,Response response)throws ExecuteException
	{
		String idString = request.getRequestParameters().get("id");
		int id = 0;
		try
		{
			id = Integer.parseInt(idString);
		}catch(Exception e)
		{
			e.printStackTrace();
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.errorPara"));
		}
		
		ConsultService service = new ConsultService();
		ConsultWay way = service.queryConsultWay(id);
		if(way == null)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.errorPara"));
		if(way.getCompany() != request.getAccount().getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noauth"));
		
		if(!service.deleteConsultWay(id))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.deleteConsultWay",way.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}
	}
	public void update(Request request,Response response)throws ExecuteException
	{
		String name = request.getRequestParameters().get("name");
		String idString = request.getRequestParameters().get("id");
		String desc = request.getRequestParameters().get("description");
		int id = 0;
		if(name == null || "".equals(name.trim()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noname"));
		try
		{
			id = Integer.parseInt(idString);
		}catch(Exception e)
		{
			e.printStackTrace();
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.errorPara"));
		}
		
		ConsultService service = new ConsultService();
		ConsultWay way = service.queryConsultWay(id);
		if(way == null)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.errorPara"));
		if(way.getCompany() != request.getAccount().getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noauth"));
		
		way.setDescription(desc);
		way.setName(name);
		
		if(!service.updateConsultWay(way))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.updateConsultWay",way.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}
	}
	public void queryAll(Request request,Response response)throws ExecuteException
	{
		ConsultService service = new ConsultService();
		List<ConsultWay> ways = service.queryAllConsultWay(request.getAccount().getCompany());
		
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(ways));
		response.setPayload(payload);
	}
	public void add(Request request,Response response)throws ExecuteException
	{
		String name = request.getRequestParameters().get("name");
		String desc = request.getRequestParameters().get("description");
		if(name == null || "".equals(name.trim()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noname"));
		
		ConsultService service = new ConsultService();
		ConsultWay way = service.queryByNameAndCompany(name, request.getAccount().getCompany());
		if(way != null)
			throw new ExecuteException(ResourceManager.getInstance().getMessage(";com.zxmr.info.consultWay.duplicateName"));
		
		ConsultWay newWay = new ConsultWay();
		newWay.setCompany(request.getAccount().getCompany());
		newWay.setDescription(desc);
		newWay.setName(name);
		
		if(!service.addConsultWay(newWay))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.addConsultWay",newWay.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}
	}
}
